#!/bin/sh

cd $(dirname $0) || exit

echo "# wsl settins
PS1='\\[\\033[01;32m\\]\${WSL_DISTRO_NAME}\\[\\033[00m\\]:\\[\\033[01;34m\\]\\W\\[\\033[00m\\]\\$ '
alias ll='ls -la'
alias npp='/mnt/d/tools/notepadpp/notepad++.exe'
" > ~/.profile

if [ ! -f /etc/apk/repositories.bak ]; then
	cp /etc/apk/repositories /etc/apk/repositories.bak
fi

# for test if there is all ready a proxy, use it
if (echo "" | nc -nvv localhost 8081) 2>/dev/nul ; then
	sed -i s@https*://dl-cdn.alpinelinux.org@http://localhost@g /etc/apk/repositories
else
	sed -i s@https*://dl-cdn.alpinelinux.org@https://mirrors.tuna.tsinghua.edu.cn@g /etc/apk/repositories
fi

apk update
apk add curl nginx openjdk8 || exit

cp stop.sh start.sh update-hosts.sh ~
chmod +x ~/start.sh ~/stop.sh ~/update-hosts.sh
cp nginx.conf /etc/nginx/nginx.conf

# for test if there is all ready a proxy, stop it
while curl -I localhost:8081 2>/dev/nul
do
	read -n 1 -p "a nexus proxy is running, stop it and press any key to continue..."
done

~/nexus-3.43.0-01/bin/nexus start || exit
nginx || exit

until curl -I localhost:8081 2>/dev/nul
do
    printf .
	sleep 1
done

printf "%-35s" "change-password admin:admin123"
curl -v -u admin:$(cat ~/sonatype-work/nexus3/admin.password) -X 'PUT' \
  'http://localhost:8081/service/rest/v1/security/users/admin/change-password' \
  -H 'accept: application/json' -H 'Content-Type: text/plain' -H 'NX-ANTI-CSRF-TOKEN: 0.7042160459096045' \
  -d 'admin123'   2> >(grep -i "< HTTP/1.1")

nexus_api='curl -v -u admin:admin123 -X'

printf "%-35s" "enable anonymous access"
$nexus_api 'PUT' \
  'http://localhost:8081/service/rest/v1/security/anonymous' \
  -H 'accept: application/json' -H 'Content-Type: application/json' -H 'NX-ANTI-CSRF-TOKEN: 0.7042160459096045' \
  -d '{
  "enabled": true,
  "userId": "anonymous",
  "realmName": "NexusAuthorizingRealm"
}'  2> >(grep -i "< HTTP/1.1")

printf "%-35s" "set active docker realms"
$nexus_api 'PUT' 'http://localhost:8081/service/rest/v1/security/realms/active' \
  -H 'accept: application/json' -H 'Content-Type: application/json' -H 'NX-ANTI-CSRF-TOKEN: 0.7042160459096045' \
  -d '[
  "DockerToken",
  "NexusAuthenticatingRealm",
  "NexusAuthorizingRealm"
]'  2> >(grep -i "< HTTP/1.1")

printf "%-35s" "create blob storage: alpine"
$nexus_api 'POST' 'http://localhost:8081/service/rest/v1/blobstores/file' \
  -H 'accept: application/json' -H 'Content-Type: application/json' -H 'NX-ANTI-CSRF-TOKEN: 0.7042160459096045' \
  -d '{
  "softQuota": null,
  "name": "alpine",
  "path": "/root/sonatype-work/nexus3/blobs/alpine"
}'  2> >(grep -i "< HTTP/1.1")

printf "%-35s" "create blob storage: ubuntu"
$nexus_api 'POST' 'http://localhost:8081/service/rest/v1/blobstores/file' \
  -H 'accept: application/json' -H 'Content-Type: application/json' -H 'NX-ANTI-CSRF-TOKEN: 0.7042160459096045' \
  -d '{
  "softQuota": null,
  "name": "ubuntu",
  "path": "/root/sonatype-work/nexus3/blobs/ubuntu"
}'  2> >(grep -i "< HTTP/1.1")

printf "%-35s" "create blob storage: pypi"
$nexus_api 'POST' 'http://localhost:8081/service/rest/v1/blobstores/file' \
  -H 'accept: application/json' -H 'Content-Type: application/json' -H 'NX-ANTI-CSRF-TOKEN: 0.7042160459096045' \
  -d '{
  "softQuota": null,
  "name": "pypi",
  "path": "/root/sonatype-work/nexus3/blobs/pypi"
}'  2> >(grep -i "< HTTP/1.1")

printf "%-35s" "create blob storage: docker"
$nexus_api 'POST' 'http://localhost:8081/service/rest/v1/blobstores/file' \
  -H 'accept: application/json' -H 'Content-Type: application/json' -H 'NX-ANTI-CSRF-TOKEN: 0.7042160459096045' \
  -d '{
  "softQuota": null,
  "name": "docker",
  "path": "/root/sonatype-work/nexus3/blobs/docker"
}'  2> >(grep -i "< HTTP/1.1")

printf "%-35s" "create repository: alpine"
$nexus_api 'POST' 'http://localhost:8081/service/rest/v1/repositories/apt/proxy' \
  -H 'accept: application/json' -H 'Content-Type: application/json' -H 'NX-ANTI-CSRF-TOKEN: 0.7042160459096045' \
  -d '{
  "name": "alpine",
  "online": true,
  "storage": {
    "blobStoreName": "alpine",
    "strictContentTypeValidation": false
  },
  "cleanup": null,
  "proxy": {
    "remoteUrl": "https://mirrors.tuna.tsinghua.edu.cn/",
    "contentMaxAge": -1,
    "metadataMaxAge": 1440
  },
  "negativeCache": {
    "enabled": false,
    "timeToLive": 1440
  },
  "httpClient": {
    "blocked": false,
    "autoBlock": true,
    "connection": null,
    "authentication": null
  },
  "routingRule": null,
  "replication": null,
  "apt": {
    "distribution": "alpine",
    "flat": false
  }
}'  2> >(grep -i "< HTTP/1.1")

printf "%-35s" "create repository: ubuntu"
$nexus_api 'POST' 'http://localhost:8081/service/rest/v1/repositories/apt/proxy' \
  -H 'accept: application/json' -H 'Content-Type: application/json' -H 'NX-ANTI-CSRF-TOKEN: 0.7042160459096045' \
  -d '{
  "name": "ubuntu",
  "online": true,
  "storage": {
    "blobStoreName": "ubuntu",
    "strictContentTypeValidation": true
  },
  "cleanup": null,
  "proxy": {
    "remoteUrl": "https://mirrors.tuna.tsinghua.edu.cn/",
    "contentMaxAge": -1,
    "metadataMaxAge": 1440
  },
  "negativeCache": {
    "enabled": false,
    "timeToLive": 1440
  },
  "httpClient": {
    "blocked": false,
    "autoBlock": true,
    "connection": null,
    "authentication": null
  },
  "routingRule": null,
  "replication": null,
  "apt": {
    "distribution": "ubuntu",
    "flat": false
  }
}'  2> >(grep -i "< HTTP/1.1")

printf "%-35s" "create repository: pypi"
$nexus_api 'POST' 'http://localhost:8081/service/rest/v1/repositories/pypi/proxy' \
  -H 'accept: application/json' -H 'Content-Type: application/json' -H 'NX-ANTI-CSRF-TOKEN: 0.7042160459096045' \
  -d '{
  "name": "pypi",
  "online": true,
  "storage": {
    "blobStoreName": "pypi",
    "strictContentTypeValidation": true
  },
  "cleanup": null,
  "proxy": {
    "remoteUrl": "https://pypi.tuna.tsinghua.edu.cn/",
    "contentMaxAge": -1,
    "metadataMaxAge": 1440
  },
  "negativeCache": {
    "enabled": false,
    "timeToLive": 1440
  },
  "httpClient": {
    "blocked": false,
    "autoBlock": true,
    "connection": null,
    "authentication": null
  },
  "routingRule": null,
  "replication": null
}'  2> >(grep -i "< HTTP/1.1")

printf "%-35s" "create repository: docker-163"
$nexus_api 'POST' 'http://localhost:8081/service/rest/v1/repositories/docker/proxy' \
  -H 'accept: application/json' -H 'Content-Type: application/json' -H 'NX-ANTI-CSRF-TOKEN: 0.7042160459096045' \
  -d '{
  "name": "docker-163",
  "online": true,
  "storage": {
    "blobStoreName": "docker",
    "strictContentTypeValidation": true
  },
  "cleanup": null,
  "proxy": {
    "remoteUrl": "http://hub-mirror.c.163.com",
    "contentMaxAge": -1,
    "metadataMaxAge": 1440
  },
  "negativeCache": {
    "enabled": false,
    "timeToLive": 1440
  },
  "httpClient": {
    "blocked": false,
    "autoBlock": true,
    "connection": null,
    "authentication": null
  },
  "routingRule": null,
  "replication": null,
  "docker": {
    "v1Enabled": true,
    "forceBasicAuth": false,
    "httpPort": null,
    "httpsPort": null,
    "subdomain": null
  },
  "dockerProxy": {
    "indexType": "HUB",
    "indexUrl": null,
    "cacheForeignLayers": true,
    "foreignLayerUrlWhitelist": [
      ".*"
    ]
  }
}'  2> >(grep -i "< HTTP/1.1")

printf "%-35s" "create repository: docker-io"
$nexus_api 'POST' 'http://localhost:8081/service/rest/v1/repositories/docker/proxy' \
  -H 'accept: application/json' -H 'Content-Type: application/json' -H 'NX-ANTI-CSRF-TOKEN: 0.7042160459096045' \
  -d '{
  "name": "docker-io",
  "online": true,
  "storage": {
    "blobStoreName": "docker",
    "strictContentTypeValidation": true
  },
  "cleanup": null,
  "proxy": {
    "remoteUrl": "https://registry-1.docker.io",
    "contentMaxAge": -1,
    "metadataMaxAge": 1440
  },
  "negativeCache": {
    "enabled": false,
    "timeToLive": 1440
  },
  "httpClient": {
    "blocked": false,
    "autoBlock": true,
    "connection": null,
    "authentication": null
  },
  "routingRule": null,
  "replication": null,
  "docker": {
    "v1Enabled": true,
    "forceBasicAuth": false,
    "httpPort": null,
    "httpsPort": null,
    "subdomain": null
  },
  "dockerProxy": {
    "indexType": "CUSTOM",
    "indexUrl": "https://index.docker.io/",
    "cacheForeignLayers": true,
    "foreignLayerUrlWhitelist": [
      ".*"
    ]
  }
}'  2> >(grep -i "< HTTP/1.1")

printf "%-35s" "create repository: docker-host"
$nexus_api 'POST' 'http://localhost:8081/service/rest/v1/repositories/docker/hosted' \
  -H 'accept: application/json' -H 'Content-Type: application/json' -H 'NX-ANTI-CSRF-TOKEN: 0.7042160459096045' \
  -d '{
  "name": "docker-host",
  "online": true,
  "storage": {
    "blobStoreName": "docker",
    "strictContentTypeValidation": true,
    "writePolicy": "ALLOW",
    "latestPolicy": false
  },
  "cleanup": null,
  "component": {
    "proprietaryComponents": false
  },
  "docker": {
    "v1Enabled": true,
    "forceBasicAuth": false,
    "httpPort": 5000,
    "httpsPort": null,
    "subdomain": null
  }
}'  2> >(grep -i "< HTTP/1.1")

printf "%-35s" "create repository: docker"
$nexus_api 'POST' 'http://localhost:8081/service/rest/v1/repositories/docker/group' \
  -H 'accept: application/json' -H 'Content-Type: application/json' -H 'NX-ANTI-CSRF-TOKEN: 0.7042160459096045' \
  -d '{
  "name": "docker",
  "online": true,
  "storage": {
    "blobStoreName": "docker",
    "strictContentTypeValidation": true
  },
  "group": {
    "memberNames": [
      "docker-host", "docker-163", "docker-io"
    ],
    "writableMember": null
  },
  "docker": {
    "v1Enabled": true,
    "forceBasicAuth": false,
    "httpPort": 8181,
    "httpsPort": null,
    "subdomain": null
  }
}'  2> >(grep -i "< HTTP/1.1")
